package com.karry.yl.web.system;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.karry.commons.base.SuperController;
import com.karry.commons.vo.JsonResponse;
import com.karry.commons.vo.LayTableResponse;
import com.karry.yl.domain.system.Resources;
import com.karry.yl.service.system.ResourcesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/resources")
public class ResourcesController extends SuperController {

    @Autowired
    private ResourcesService resourcesService;


    /**
     * 资源管理添加页
     * @return
     */
    @RequestMapping("/ui/add")
    public String addUI(){
       return "system/resources/add";
    }


    /**
     * 资源管理编辑页
     * @return
     */
    @RequestMapping("/ui/edit")
    public String editUI(Long id, ModelMap modelMap){
        Resources resources = resourcesService.selectById(id);
        modelMap.addAttribute("resources",resources);
        if(resources.getType() != Resources.TYPE_MODULE){
            List<Resources> modules = resourcesService.selectList(new EntityWrapper<Resources>().eq("type", Resources.TYPE_MODULE).eq("deleted",false));
            modelMap.addAttribute("modules",modules);
            if(resources.getType() == Resources.TYPE_SUB_PAGE || resources.getType() == Resources.TYPE_OPT){
                List<Resources> pages = resourcesService.selectList(new EntityWrapper<Resources>().between("type",Resources.TYPE_PAGE,Resources.TYPE_SUB_PAGE).eq("parentModule",resources.getParentModule()).eq("deleted",false));
                modelMap.addAttribute("pages",pages);
            }
        }
        return "system/resources/edit";
    }

    /**
     * 资源管理列表页
     * @return
     */
    @RequestMapping("/ui/list")
    public String listUI(){
        return "system/resources/list";
    }


    @RequestMapping("/list")
    @ResponseBody
    public LayTableResponse listUI(int page, int limit){
        Page<Resources> resourcesPage = resourcesService.selectPage(new Page<Resources>(page, limit),new EntityWrapper<Resources>().orderBy("sortNo").eq("deleted",false));
        return new LayTableResponse(resourcesPage.getRecords(),resourcesPage.getTotal());
    }

    /**
     * 添加资源
     * @param resources
     * @return
     */
    @RequestMapping(path = "/save",method = RequestMethod.POST)
    public String save(Resources resources){
        resourcesService.insert(resources);
        return "redirect:/resources/ui/list";
    }

    /**
     * 修改资源
     * @param resources
     * @return
     */
    @RequestMapping(path = "/update",method = RequestMethod.POST)
    public String update(Resources resources){
        resourcesService.updateById(resources);
        return "redirect:/resources/ui/list";
    }

    /**
     * 删除
     * @param
     * @return
     */
    @RequestMapping(path = "/delete")
    public String delete(@RequestParam("id") List<Long> ids){
        resourcesService.deleteBatchIds(ids);
        return "redirect:/resources/ui/list";
    }


    /**
     * 获取所有模块
     * @return
     */
    @RequestMapping("/modules")
    @ResponseBody
    public JsonResponse modules(){
        List<Resources> modules = resourcesService.selectList(new EntityWrapper<Resources>().eq("type", Resources.TYPE_MODULE).eq("deleted",false));
        return JsonResponse.success("OK",modules);
    }

    /**
     * 获取某个模块下的页面(主页面及附属页面)
     * @return
     */
    @RequestMapping("/pages")
    @ResponseBody
    public JsonResponse pages(String moduleCode){
        List<Resources> pages = resourcesService.selectList(new EntityWrapper<Resources>().between("type",Resources.TYPE_PAGE,Resources.TYPE_SUB_PAGE).eq("parentModule",moduleCode).eq("deleted",false));
        return JsonResponse.success("OK",pages);
    }
}
